package minh.bac.wcbrazil2014.favorite;

import java.util.ArrayList;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;

import minh.bac.wcbrazil2014.R;
import minh.bac.wcbrazil2014.WCTAG;
import minh.bac.wcbrazil2014.adapter.FavoriteGridAdapter;
import minh.bac.wcbrazil2014.database.MySQLiteManager;
import minh.bac.wcbrazil2014.object.FootBallTeamObj;
import minh.bac.wcbrazil2014.object.GridFavoriteObject;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.GridView;
import android.widget.Toast;

public class ChooseFavoriteTeamFM extends Fragment{
	
	GridView gridView;
	Button doneBtn;
	private ArrayList<GridFavoriteObject> mArrayListData;
	FavoriteGridAdapter adapter;
	private MySQLiteManager mMySQLiteManager;
	DisplayImageOptions displayImageOptions;
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		View rootView = inflater.inflate(R.layout.choosefavorite_layout, container, false);
		gridView = (GridView) rootView.findViewById(R.id.choosefavorite_gridv);
		doneBtn = (Button) rootView.findViewById(R.id.choosefavorite_donebtn);
		mMySQLiteManager = new MySQLiteManager(getActivity());
		mArrayListData =  new ArrayList<GridFavoriteObject>();
		displayImageOptions = new DisplayImageOptions.Builder()
		.cacheInMemory(false)
		.cacheOnDisc(true)		
		.resetViewBeforeLoading(false)
		.bitmapConfig(Bitmap.Config.RGB_565)										
		.showImageForEmptyUri(R.drawable.ic_emblem_club_placeholder)
		.showImageOnFail(R.drawable.ic_emblem_club_placeholder)		
		.imageScaleType(ImageScaleType.EXACTLY)										
		.build();	
		//
		LoadData();
		//
		adapter = new FavoriteGridAdapter(getActivity(), R.layout.choose_favorite_item, mArrayListData, displayImageOptions);
		gridView.setAdapter(adapter);
		doneBtn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				ArrayList<FootBallTeamObj> arrayList = new ArrayList<FootBallTeamObj>();
				arrayList = mMySQLiteManager.getFavoriteTeam();
				if(arrayList != null && arrayList.size() != 0)
				{
					FavoriteFragment.switchFragment(R.id.container_favorite, new ListViewTeamFM() , true , WCTAG.TAGFM_FAVORITE);
				}
				else
				{
					Toast.makeText(getActivity(), "Vui lòng chọn đội yêu thích", Toast.LENGTH_SHORT).show();
				}
			}
		});
		return rootView;
	}
	
	public void LoadData()
	{
		ArrayList<FootBallTeamObj> ballTeamObjs = new ArrayList<FootBallTeamObj>();
		ballTeamObjs = mMySQLiteManager.getAllFB();
		if(ballTeamObjs != null)
		{
			String imgLinkl = "";
			for(int i =0; i < ballTeamObjs.size(); i++)
			{
				imgLinkl = mMySQLiteManager.getLinkImgByName(ballTeamObjs.get(i).getDoiBong());
				if(imgLinkl == null || imgLinkl.equals(""))
				{
					imgLinkl = mMySQLiteManager.getLinkImgByName2(ballTeamObjs.get(i).getDoiBong());
				}
				if(imgLinkl != null)
					mArrayListData.add(new GridFavoriteObject(imgLinkl, ballTeamObjs.get(i)));
			}			
		}
		
	}
	

}
